package nfwx.analytics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

/**
 * 工具函数，用于HTTP访问
 */

public class GrabHttp {
    public static String httpGET(String url) {
        InputStream inputStream=null;
        URLConnection conn=null;
        try {
            URL u = new URL(url);
            conn = u.openConnection();
            if (conn instanceof HttpURLConnection) {
                HttpURLConnection http = (HttpURLConnection)conn;
                http.setRequestMethod("GET");
                http.setConnectTimeout(10000);
                http.connect();
                inputStream = http.getInputStream();
            } else if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection https = (HttpsURLConnection)conn;
                https.setRequestMethod("GET");
                https.setConnectTimeout(10000);
                https.connect();
                inputStream = https.getInputStream();
            } else {
                throw new IOException("Not supported connection type");
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String readerLine;
            StringBuffer buffer = new StringBuffer();
            while ((readerLine = reader.readLine()) != null) {
                buffer.append(readerLine);
            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static String httpPOST(String url, Map<String,String> params) {
        InputStream inputStream=null;
        OutputStream out=null;
        URLConnection conn=null;
        try {
            URL u = new URL(url);
            conn = u.openConnection();
            if (conn instanceof HttpURLConnection) {
                HttpURLConnection http = (HttpURLConnection)conn;
                http.setRequestMethod("POST");
                http.setConnectTimeout(10000);
                http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                http.setRequestProperty("Connection", "Keep-Alive");
                http.connect();
            } else if (conn instanceof HttpsURLConnection) {
                HttpsURLConnection https = (HttpsURLConnection)conn;
                https.setRequestMethod("POST");
                https.setConnectTimeout(10000);
                https.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                https.setRequestProperty("Connection", "Keep-Alive");
                https.connect();
            } else {
                throw new IOException("Not supported connection type");
            }
            StringBuilder sb = new StringBuilder();
            for(Map.Entry<String,String> kv: params.entrySet()) {
                if (sb.length() != 0) sb.append('&');
                sb.append(URLEncoder.encode(kv.getKey(), "UTF-8"));
                sb.append('=');
                sb.append(URLEncoder.encode(kv.getValue(), "UTF-8"));
            }
            out = conn.getOutputStream();
            out.write(sb.toString().getBytes("UTF-8"));
            out.flush();
            out.close();
            out = null;
            inputStream = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String readerLine;
            StringBuffer buffer = new StringBuffer();
            while ((readerLine = reader.readLine()) != null) {
                buffer.append(readerLine);
            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
